package com.xaicode.template.rabbitmq.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

import static com.xaicode.template.rabbitmq.common.RabbitConst.*;

/**
 * @author Locker
 * @date 2021/8/20 0020 17:12
 */
@Slf4j
@Service
public class RabbitService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void broadcast(String msg) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(FANOUT_EXCHANGE, "", msg, correlationData);
    }

    public void direct(String msg) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        /*
            channel         The RabbitMQ Channel to operate within.
            routingKey      The routing key.
            message         The Message to send.
            correlationData The correlation data.
         */
        rabbitTemplate.convertAndSend(DIRECT_EXCHANGE, DIRECT_ROUTING_KEY, msg, correlationData);
    }

}
